<?php
class ShopAction extends Action{
	public function shop(){
		
		//网站配置
		$config = M('Config')->find();
		if($_GET['p']){
			$config['title'] = '拍卖-第'.(int)$_GET['p'].'页-'.$config['stitle'];
		}else{
			$config['title'] = '拍卖-'.$config['stitle'];
		}
		$config['keywords'] = '拍卖-'.$config['keywords'];
		$this->assign('config',$config);
		/*所有*/
		$time = strtotime($_POST['time']);
		$tem=time();
		if(!$time){
			$shop = M('Shop')->order('endtime DESC')->where("state = 1 AND endtime > $tem")->select();
			
		}else{
			$shop = M('Shop')->order('endtime DESC')->where("state = 1 AND endtime > $time")->select();
			
		}
		foreach($shop as $key=>$val){
				$newid=$val['sid'];
				$qprce=M('Shop_buy')->field('price')->where("sid=$newid")->order('price desc')->find();
				$count=M('Shop_buy')->where("sid=$newid")->count();
				$shop[$key]['newprice']=$qprce['price'];
				$shop[$key]['count']=$count;
		}
		$this->assign('shop',$shop);
		/*正在*/
		$now = M('Shop')->order('addtime DESC')->where('state = 1 AND endtime > '.time())->select();
		$this->assign('now',$now);
		/*近期*/
		$stime = time()-7*24*3600;
		$etime = time()+7*24*3600;
		$near = M('Shop')->where("state = 1 AND endtime > $stime OR state = 1 AND endtime < $etime")->order('endtime DESC')->select();
		$this->assign('near',$near);
		$this->display();
	}
	
	public function shop_info(){
		
		//网站配置
		$config = M('Config')->find();
		if($_GET['p']){
			$config['title'] = '拍卖-第'.(int)$_GET['p'].'页-'.$config['stitle'];
		}else{
			$config['title'] = '拍卖-'.$config['stitle'];
		}
		$config['keywords'] = '拍卖-'.$config['keywords'];
		$this->assign('config',$config);
		/*商品信息*/
		$id = (int)$_GET['id'];
		$shop = M('Shop')->where("sid = $id")->find();
		if(!$shop){
			$this->error('不存在此拍品哦');
		}
		$this->autoprice();
		if($rescou=M('Shop_buy')->field('price')->where("sid=$id")->order('buytime desc')->find()){
			$shop['c_price']=$rescou['price'];
		}
		$shop['id']=$id;
		$shop['user'] = explode('#',$shop['user']);
		$shop['username'] = $shop['user'][1];
		$shop['userid'] = $shop['user'][0];
		$shop['less_price'] = $shop['c_price']+$shop['step'];
		$this->assign('shop',$shop);
		/*用户评论*/
		$com = M('shop_com')->where("state = 1 AND　sid = $id")->order('cid DESC')->select();
		foreach($com as $key=>$value){
			$user= explode('#',$value['user']);
			$buy[$key]['username']=$user[1];
		}
		/*喊价记录*/
		$buy=S('jingpai'.$id);
		if($buy==''){
			S('jingpai'.$id,M('Shop_buy')->where("sid=$id")->select());
			$buy=S('jingpai'.$id);
		}
		$this->assign('buy',$buy);
		/*正在*/
		$now = M('Shop')->order('addtime DESC')->where('state = 1 AND endtime > '.time())->select();
		$this->assign('now',$now);
		/*近期*/
		$stime = time()-7*24*3600;
		$etime = time()+7*24*3600;
		$near = M('Shop')->where("state = 1 AND endtime > $stime OR state = 1 AND endtime < $etime")->order('endtime DESC')->select();
		$this->assign('near',$near);
		$this->display();
		}
		public function addprice(){
			if(!$_SESSION['uid']){ 
				echo 2;
			}else{
			$uid=M('Shop_buy')->field('uid')->where($_POST['sid'])->order('bid desc')->find();
			if($uid['uid']==$_SESSION['uid']){	echo '-1';}else{
				$sid=(int)$_POST['sid'];
				$end=M('Shop')->field('endtime')->where("sid=$sid")->find();
				$endt=date("YmdHis",$end['endtime']);
				$dtime=date("YmdHis",time());
				$pric=(int)$_POST['price'];
				$spric=$_SESSION['score'];
				if($endt>$dtime){
					if($pric<$spric){
					$data['price'] = (int)$_POST['price'];//价格
					$data['uid'] = $_SESSION['uid'];
					$data['sid'] = (int)$_POST['sid'];//商品ID
					$data['user'] = $_SESSION['uname'];
					$data['buytime']=time();  //竞拍时间
					//删除用户积分;将用户积分移至到竞拍表中
					$uid=$_SESSION['uid'];
						if(M('User')->where("uid = $uid")->setDec('score',$pric)!=false){
							$jilu=M('Shop_buy');//竞拍记录表
							if($jilu->add($data)!=false){
								$sia['price']='DESC';
								$sia['buytime']='DESC';
								$scrouce=$jilu->field('user,price,buytime')->where($data['sid'])->order($sia)->select();
								S('jingpai'.$data['sid'],$scrouce);
								S('jingpai'.$data['sid']);
								echo '出价成功';
							}
						}
					}else{
						echo '你的人品太差了，赶紧去赚取人品吧';
					}
					
				}else{
					echo '竞拍已结束';
				}
			}
		}
		
	}	
	/*	留言添加	*/
	
		
	
	/*获取最新竞拍价*/
	public function prices(){
		 $buy=S('jingpai');
		 foreach($buy as $v){
			$str.='<td>'.$v['user'].'</td>'.'<td>'.$v['price'].'</td>'.'<td>'.date('Y-m-d H:i:s',$v['buytime']).'</td>';
		 }
		 echo json_encode($str);
	}
	/*自动扣除商品积分*/
	private function autoprice(){
		$sid=$_GET['id'];
		$endt=M('shop')->field('endtime')->where("sid=$sid")->find();
		if($endt['endtime']<time()){
			$arr=M('Shop_buy')->field('uid,price')->where("sid =$sid")->order('price desc')->select();
			$i=0;
			foreach($arr as $key=>$val){
				if($i!=0){
					$uid=$val['uid'];
					$pec=$val['price'];
					M('User')->where("uid=$uid")->setInc('score',$pec);
				}else{
					$ed['uid']=$val['uid'];
					$ed['price']=$val['price'];
					$ed['sid']=$val['sid'];
					$ed['user']=$val['user'];
					$ed['comtime']=time();
					M('shop_com')->add($ed);
				}
				$i++;
			}
		}
	}
	
}
?>